<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2020/1/11
 * Time: 10:41
 */

namespace app\index\controller;
use think\captcha\Captcha;
use think\Cookie;
use app\api\controller\v1\User;

class VerifyCode
{
    public function getCode(){
        //判断当前客户端是否有一个验证码标识 ,如果有则使用现有,如果没有则直接生成一个新的
        if(!isset($_COOKIE['verify_sign'])){
            //生成一个客户端验证码的标识码
            $sign = md5(time().mt_rand(1000,9999));
            cookie('verify_sign',$sign,30*24*60*60);
        }else{
            $sign = Cookie::get('verify_sign');
        }

        $captcha = new Captcha();
//        print_r($captcha);
        return $captcha->entry($sign);
    }

    public function checkCode(){
        $code = input("code"); //或取验证码参数

        $name=input("userName");
        $password=input("password");
        $email=input("email");
        $data=[
            "userName"=>$name,
            "password"=>$password,
            "email"=>$email
        ];
        $captcha = new Captcha();//实例化验证码生成验证器
        $sign = Cookie::get('verify_sign');//或取身份标识
        if($captcha->check($code, $sign)){//实例化后生成验证器
            $User= new User;
            $User->register($data);
            return '验证码输入正确!';
        }else{
            return '验证码输入错误!';
        }
    }
}